package com.ffcs.cn.action.base.treeTable;

import com.supermap.desktop.core.ui.controls.GridBagConstraintsHelper;
import com.supermap.desktop.process.ProcessResources;
import org.jetbrains.userdefine.ui.speedSearch.SimpleColoredComponent;

import javax.swing.*;
import java.awt.*;

/**
  * 参见DemoJTreeTableTreeCellRenderCheck注释
 */
public class PanelCheck extends JPanel {

	private JCheckBox checkBox = new JCheckBox();
	private SimpleColoredComponent labelText = new SimpleColoredComponent();
	private SimpleColoredComponent labelImage = new SimpleColoredComponent();

	public PanelCheck(TreeTableData treeTableData) {
//		setLayout(new FlowLayout(FlowLayout.TRAILING, 0, 0));
		this.checkBox.setSelected(treeTableData.isSelected());
		this.checkBox.setBorder(BorderFactory.createEmptyBorder());
		this.labelText.setText(treeTableData.getValue());
		if (treeTableData.getValue().length() == 3) {
			this.labelImage.setIcon(ProcessResources.getIcon("/processresources/Variable.svg"));
		} else if (treeTableData.getValue().length() == 2) {
			this.labelImage.setIcon(ProcessResources.getIcon("/processresources/InputParameter.svg"));
		} else {
			this.labelImage.setIcon(ProcessResources.getIcon("/processresources/WorkFlow/WorkFlow.png"));
		}
		setLayout(new GridBagLayout());
		this.add(this.checkBox, new GridBagConstraintsHelper(0, 0, 1, 1).setInsets(0, 0).setFill(GridBagConstraints.NONE).setAnchor(GridBagConstraintsHelper.WEST));
		this.add(this.labelText, new GridBagConstraintsHelper(1, 0, 1, 1).setInsets(0, 0).setFill(GridBagConstraints.NONE).setAnchor(GridBagConstraintsHelper.WEST));
		this.add(this.labelImage, new GridBagConstraintsHelper(2, 0, 1, 1).setInsets(0, 0).setFill(GridBagConstraints.NONE).setAnchor(GridBagConstraintsHelper.WEST));
		this.add(new JLabel(), new GridBagConstraintsHelper(3, 0, 1, 1).setInsets(0, 0).setFill(GridBagConstraints.HORIZONTAL).setAnchor(GridBagConstraintsHelper.WEST).setWeight(1,1));

//		add(this.checkBox);
//		add(this.labelText);
//		add(this.labelImage);
		setBorder(BorderFactory.createEmptyBorder());
	}


	/**
	 * 判断鼠标x坐标是否位于复选框内，复选框是第一个控件，所以判断标准是>=0 and <=复选框宽度
	 * @param x  鼠标x坐标
	 * @return 是否命中复选框
	 */
	public boolean hitCheckBox(int x) {
		return x>=0 && x<=this.checkBox.getPreferredSize().width;
	}

	/**
	 * 判断鼠标x坐标是否位于最后的图标内，图标是最后一个控件，所以判断标准是>=前两个控件宽度 and <=前两个控件宽度+图标宽度
	 * @param x  鼠标x坐标
	 * @return 是否命中最后一个图标
	 */
	public boolean hitLastImage(int x) {
		if (this.labelImage.isVisible()) {
			int startX = this.checkBox.getPreferredSize().width + this.labelText.getPreferredSize().width;
			return x >= startX && x <= startX + this.labelImage.getPreferredSize().width;
		}
		return false;
	}

	public SimpleColoredComponent getLabelText() {
		return this.labelText;
	}

	public void setLabelImageVisible(boolean visible) {
		this.labelImage.setVisible(visible);
	}
}
